<template>
  <div>
    <el-form :inline="true" ref="searchMap" :model="searchMap" class="demo-form-inline">
      <el-form-item prop="username">
        <el-input v-model="searchMap.username" placeholder="账号"></el-input>
      </el-form-item>
      <el-form-item prop="name">
        <el-input v-model="searchMap.name" placeholder="姓名"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="search">查询</el-button>
        <el-button type="primary" @click="add">新增</el-button>
        <el-button @click="resetForm('searchMap')">重置</el-button>
      </el-form-item>
    </el-form>
    <el-table :data="list" border style="width: 100%" height="470px">
      <el-table-column label="序号" align="center" type="index"></el-table-column>
      <el-table-column prop="username" label="账号"></el-table-column>
      <el-table-column prop="name" label="姓名"></el-table-column>
      <el-table-column prop="age" label="年龄"></el-table-column>
      <el-table-column prop="mobile" label="电话"></el-table-column>
      <el-table-column prop="salary" label="薪酬"></el-table-column>
      <el-table-column prop="entryDate" label="入职时间"></el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button size="mini" @click="handleEdit(scope.row.id)">编辑</el-button>
          <el-button size="mini" type="danger" @click="handleDelete(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="page"
      :page-sizes="[10, 20, 30, 40]"
      :page-size="10"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    ></el-pagination>
    <el-dialog title="员工编辑" :visible.sync="dialogFormVisible" width="500px">
      <el-form :model="addForm" style="width:400px" ref="addForm" :rules="rules">
        <el-form-item label="账号" :label-width="formLabelWidth" prop="username">
          <el-input v-model="addForm.username" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="姓名" :label-width="formLabelWidth" prop="name">
          <el-input v-model="addForm.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="年龄" :label-width="formLabelWidth" prop="age">
          <el-input v-model="addForm.age" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="电话" :label-width="formLabelWidth" prop="mobile">
          <el-input v-model="addForm.mobile" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="薪酬" :label-width="formLabelWidth" prop="salary">
          <el-input v-model="addForm.salary" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="入职时间" :label-width="formLabelWidth" prop="entryDate">
          <el-date-picker
            v-model="addForm.entryDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择入职时间"
          ></el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button
          type="primary"
          @click="addForm.id===null?addList('addForm'):editList('addForm')"
        >确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import staffApi from "@/api/staff";
export default {
  data() {
    return {
      page: 1,
      size: 10,
      total: 0,
      searchMap: {},
      list: [],
      addForm: {
        id: null,
        username: "",
        name: "",
        age: "",
        mobile: "",
        salary: "",
        entryDate: ""
      },
      dialogFormVisible: false,
      formLabelWidth: "90px",
      rules: {
        username: [
          { required: true, message: "账号不能为空", trigger: "blur" }
        ],
        name: [{ required: true, message: "姓名不能为空", trigger: "blur" }]
      }
    };
  },

  components: {},
  created() {
    this.search();
  },
  methods: {
    search(page, size, searchMap) {
      staffApi.search(this.page, this.size, this.searchMap).then(res => {
        const resp = res.data;
        if (resp.flag) {
          this.list = resp.data.rows;
          this.total = resp.data.total;
        } else {
          // 获取失败
        }
      });
    },
    handleEdit(id) {
      this.add();
      staffApi.getById(id).then(res => {
        const resp = res.data;
        if (resp.flag) {
          this.addForm = resp.data;
        } else {
          //查询失败
        }
      });
    },
    handleDelete(id) {
      this.$confirm("确认删除这条记录吗?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
      })
        .then(() => {
          staffApi.delete(id).then(res => {
            const resp = res.data;
            if (resp.flag) {
              this.$message({
                message: "删除成功",
                type: "success"
              });
            } else {
              this.$message({
                message: "删除失败",
                type: "error"
              });
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    },
    handleSizeChange(val) {
      this.size = val;
      this.search();
    },
    handleCurrentChange(val) {
      this.page = val;
      this.search();
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    add() {
      this.dialogFormVisible = true;
      this.$nextTick(() => {
        this.$refs["addForm"].resetFields();
      });
    },
    addList(formName) {
      this.$refs[formName].validate(val => {
        if (val) {
          staffApi.addList(this.addForm).then(res => {
            const resp = res.data;
            if (resp.flag) {
              this.$message({
                message: "新增成功,初始密码为123456",
                type: "warning"
              });
              this.dialogFormVisible = false;
              this.search();
            } else {
              this.$message({
                message: "新增失败",
                type: "error"
              });
            }
          });
        }
      });
    },
    editList(formName) {
      this.$refs[formName].validate(val => {
        if (val) {
          staffApi.edit(this.addForm).then(res => {
            const resp = res.data;
            if (resp.flag) {
              this.$message({
                message: "修改成功",
                type: "success"
              });
              this.dialogFormVisible = false;
              this.search();
            } else {
              this.$message({
                message: "修改失败",
                type: "error"
              });
            }
          });
        }
      });
    }
  }
};
</script>

<style scoped>
</style>